home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: ncrgw2.ncr.com!ncrhub6!daynews!falcon!news
- From: Dick Menninger <Dick.Menninger@DaytonOH.ATTGIS.COM>
- Subject: Re: Exceptions vs. assertions
- X-Nntp-Posting-Host: 149.25.99.44
- Message-ID: <DKxrB1.63K@falcon.daytonoh.attgis.com>
- Sender: news@falcon.daytonoh.attgis.com (News administrative Login)
- Reply-To: Dick.Menninger@DaytonOH.ATTGIS.COM (mennid)
- Organization: AT&T Global Information Solutions
- X-Newsreader: DiscussIT 2.5.1.3 for MS Windows [AT&T Software Products Division]
- References: <4ci0on$77p@news2.ios.com>
- Date: Tue, 9 Jan 1996 22:47:24 GMT
-
-
- > ==========lalit gidwani, 1/4/96==========
- >
- > Dick Menninger (Dick.Menninger@DaytonOH.ATTGIS.COM) wrote:
-
- [Stuff deleted]
-
- > Thanks Dick!
-
- > I have a few simple lines of code. Do you think
- > the C++ exception mechanism has been properly used here:
-
- > class SaveError; // exception class
-
- > void GetDataFromScreen() throw();
- > void SaveDataInDatabase() throw( SaveError );
- > void SaveCurrentInvoice() throw( SaveError );
-
- As long as these functions all catch all exceptions
- and convert them into SaveError, this is OK.
- Otherwise, an exception from allocation somewhere
- will terminate the program without telling the user.
-
- > int main( int argc, char **argv )
- > {
- > /*****************
- >
- > Send the selected invoice to screen and
- > wait for user to say he wants to
- > save the current invoice.
- >
- > ******************/
- >
- > try
- > {
- > SaveCurrentIncoice();
-
- Do you want to tell user success here?
-
- > }
- > catch( SaveError se )
- > {
- > // send error message to the user
- > }
- >
- > }// main
-
- From the limited specification provided, it is not
- clear at all whether this is the best expression
- of the problem. It may make more sense for
- SaveCurrentInvoice() to return a boolean of
- success==true and failure==false. Then
- the use of an exception, if any, would be
- internal to the saving algorithm, assuming
- that is the best expression of what needs
- to be done there. Even that cannot be determined
- from your limited spec.
-
-
- > void SaveCurrentInvoice() throw ( SaveError )
- > {
- > GetDataFromScreen() // always succeeds in this system
- > try
- > {
- > SaveDataToDatabase();
- > }
- > catch( SaveError err )
- > {
-
- was anything else expected to be done here?
-
- > throw err;
- > }
- > }
-
- This is most likely code bloat. What purpose
- were you trying to serve by catching and rethrowing?
-
-
- > If this code seems OK, I have two comments:
-
- > 1. It seems to increase the amount of code. However, it seems
- > that the exception-handling would reduce code if there
- > where many lines of code in a try block that could throw
- > exceptions.
- > 2. This seems to be an issue that needs to be thought out well.
-
- > P.S. The syntax may not be absolutely correct.
-
-
- > Thanx
- > LG
-
- The key point is that looking for a "best expression"
- of a problem is an ideal and requires more information
- than taking any old valid expression of the problem.
- In general, we never know if the expression is best,
- but we often know that we have found a much better
- expression than our earlier attempts when we try.
- This leads to our growing an experience base of
- patterns of solutions that we recognize more readily
- and raise the quality of this form of endeavor as we
- attack new problems.
-
- There is only a rather limited experience base with
- possible patterns involving exceptions. Everyone,
- including the language definers are still groping with
- their basic implications. As part of the "future shock"
- being caused adding them to the language, people
- are taking all kinds of very restricted views towards
- them and are applying them badly and then saying
- it is the fault of exceptions. I am trying to add what
- seems to be needed tension in the other direction
- by making what seem to me to be basic obvious
- statements.
-
- One such is stating that this is a control paradigm.
- Some want to say that it only an error paradigm.
- Well that is mixing what it is with how you want
- to [artifically, at least to me] limit use of it. This
- difference has hurt how well it was concieved
- when it was added to the language.
-
-
- Good Day
- Dick
- Dick.Menninger@DaytonOH.ATTGIS.COM
-
-